<template>
  <el-dialog
    :title="'查看'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="100%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="120px">
      <div>
      <el-row>
        <el-form-item label="销售订单名" prop="name">
          <el-input v-model="dataForm.name"  :disabled="true">
          </el-input>
        </el-form-item>
        <el-form-item label="客户" prop="clientName">
          <el-input v-model="dataForm.clientName"  :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="职员" prop="staffName">
          <el-input v-model="dataForm.staffName"  :disabled="true"></el-input>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="销售日期" prop="saleDate">
          <el-input v-model="dataForm.saleDate"  :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="交货日期" prop="deliveryDate">
          <el-input v-model="dataForm.deliveryDate" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="安装地址" prop="productAddress">
          <el-input v-model="dataForm.productAddress"  :disabled="true"></el-input>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="dataForm.remark" ></el-input>
        </el-form-item>

      </el-row>
        <el-form>
          <el-table
            :data="dataList"
            border
            :summary-method="getSummaries"
            show-summary
            v-loading="dataListLoading"
            style="width: 100%; margin-top: 20px">
            <el-table-column
              type="selection"
              header-align="center"
              align="center"
              width="50">
            </el-table-column>
            <el-table-column
              prop="commodityName"
              header-align="center"
              align="center"
              width="250px"
              label="商品名称">
              <template slot-scope="scope">
                <span>{{scope.row.commodityName}}</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="productName"
              header-align="center"
              align="center"
              width="250px"
              label="生产名称">
              <template slot-scope="scope">
                <span>{{scope.row.productName}}</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="height"
              header-align="center"
              align="center"
              label="高">
              <template slot-scope="scope">
                <span>{{scope.row.height}}</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="width"
              header-align="center"
              align="center"
              label="宽">
              <template slot-scope="scope">
                <span>{{scope.row.width}}</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="count"
              header-align="center"
              align="center"
              label="数量">
              <template slot-scope="scope">
                <span>{{scope.row.count}}</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="squares"
              header-align="center"
              align="center"
              label="面积">
              <template slot-scope="scope">
                <span>{{scope.row.squares}}</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="direction"
              header-align="center"
              align="center"
              width="150px"
              label="方向">
              <template slot-scope="scope">
                <span>{{scope.row.direction}}</span>
              </template>
            </el-table-column>

            <el-table-column
              prop="materialName"
              header-align="center"
              align="center"
              width="150px"
              label="板材">
              <template slot-scope="scope">
                <span>{{scope.row.materialName}}</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="colorName"
              header-align="center"
              align="center"
              width="150px"
              label="颜色">
              <template slot-scope="scope">
                <span>{{scope.row.colorName}}</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="crafts"
              header-align="center"
              align="center"
              width="150px"
              label="特殊工艺名称">
              <template slot-scope="scope">
                <span>{{scope.row.crafts}}</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="remark"
              header-align="center"
              align="center"
              width="150px"
              label="备注">
              <template slot-scope="scope">
                <span>{{scope.row.remark}}</span>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :current-page="pageIndex"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pageSize"
            :total="totalPage"
            layout="total, sizes, prev, pager, next, jumper">
          </el-pagination>
        </el-form>
      </div>
    </el-form>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          status: '',
          deleted: '',
          createTime: '',
          createUserId: '',
          tenantId: '',
          productName: '',
          headerId: 0,
          name: '',
          code: '',
          clientId: '',
          clientName: '',
          staffId: '',
          staffName: '',
          saleDate: '',
          deliveryDate: '',
          storehouseId: 0,
          productAddress: '',
          remark: '',
          discountRate: 0,
          discountAmount: '',
          warning: 0,
          totalAmount: '',
          deposit: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        dataRule: {
          status: [
            {required: true, message: '状态，0：新增，1：缓冲，2：待结算，3：已结算，4：生产中不能为空', trigger: 'blur'}
          ],
          deleted: [
            {required: true, message: '删除标志，0：未删除，1：删除不能为空', trigger: 'blur'}
          ],
          createTime: [
            {required: true, message: '创建时间不能为空', trigger: 'blur'}
          ],
          createUserId: [
            {required: true, message: '创建者ID不能为空', trigger: 'blur'}
          ],
          tenantId: [
            {required: true, message: '租户ID不能为空', trigger: 'blur'}
          ],
          productName: [
            {required: true, message: '生产名称不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/pro/order/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.productName = data.order.productName
                this.dataForm.name = data.order.name
                this.dataForm.clientId = data.order.clientId
                this.dataForm.clientName = data.order.clientName
                this.dataForm.staffId = data.order.staffId
                this.dataForm.staffName = data.order.staffName
                this.dataForm.saleDate = data.order.saleDate
                this.dataForm.deliveryDate = data.order.deliveryDate
                // todo
                // this.dataForm.storehouseId = data.order.storehouseId
                this.dataForm.storehouseId = 0
                this.dataForm.productAddress = data.order.productAddress
                this.dataForm.remark = data.order.remark
                this.dataForm.discountRate = data.order.discountRate
                this.dataForm.discountAmount = data.order.discountAmount
                this.dataForm.totalAmount = data.order.totalAmount
                this.dataForm.deposit = data.order.deposit
                this.dataForm.status = data.order.status
                this.dataForm.warning = data.order.warning

                this.getDataListByHeadId(this.dataForm.id)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/pro/order/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'status': this.dataForm.status,
                'deleted': this.dataForm.deleted,
                'createTime': this.dataForm.createTime,
                'createUserId': this.dataForm.createUserId,
                'tenantId': this.dataForm.tenantId,
                'productName': this.dataForm.productName
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 获取数据列表
      getDataListByHeadId (id) {
        this.dataForm.headerId = id || 0
        this.dataListLoading = true
        this.delList = []
        this.$http({
          url: this.$http.adornUrl(`/pro/order/listAllByHeadId`),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'headId': id
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
            this.$message.error(data.msg)
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataListByHeadId(this.dataForm.headerId)
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataListByHeadId(this.dataForm.headerId)
      },
      // 合计
      getSummaries (param) {
        const {columns, data} = param
        const sums = []
        columns.forEach((column, index) => {
          if (index === 1) {
            sums[index] = '合计'
            return
          }
          const values = data.map(item => Number(item[column.property]))
          // 销售金额(元)
          if (column.property === 'salesAmount') {
            if (!values.every(value => isNaN(value))) {
              sums[index] = values.reduce((prev, curr) => {
                const value = Number(curr)
                if (!isNaN(value)) {
                  return prev + curr
                } else {
                  return prev
                }
              }, 0)
              sums[index] += ''
            } else {
              sums[index] = ''
            }

            sums[index] = parseFloat(sums[index]).toFixed(2)

            this.salesAmount = sums[index]
          }
          // 数量
          if (column.property === 'count') {
            if (!values.every(value => isNaN(value))) {
              sums[index] = values.reduce((prev, curr) => {
                const value = Number(curr)
                if (!isNaN(value)) {
                  return prev + curr
                } else {
                  return prev
                }
              }, 0)
              sums[index] += ''
            } else {
              sums[index] = ''
            }
          }
        })
        return sums
      }
    }
  }
</script>
<style lang="scss" scoped>
  .select-option {
    width: 600px;
  }
</style>
